package apps.employeeapp;

public abstract class User {

    // constructor.
    public User() {

    }

    protected abstract String getUsername();

    protected abstract String getPassword();

    public abstract boolean isManager();

    public boolean hasAccess() {
        // to just demonstrate that this check will go to concrete class.
        if (!isValidUser(getUsername())) {
            throw new IllegalArgumentException("username is not valid");
        }
        if (getDBPassword(getUsername()).equals(getPassword())) {
            return true;
        }
        return false;
    }

    // sub class can override and change behaviour. // eg for director there is no such kind of validation.
    // so in director class we will override this and return always true.
    public boolean isValidUser(String username) {
        // to just demonstrate that this check will go to concrete class.
        if (isManager()) {
            return username.startsWith("M");
        } else {
            return username.startsWith("E");
        }
    }

    // dummy method to show the flow. this return the password of the user.
    // works as a stub for get th password from database.
    private String getDBPassword(String username) {
        String storedPassword = null;
        if (username.equals("E12345")) {
            storedPassword = "test123";// stored password for that user
        } else if (username.equals("M12346")) {
            storedPassword = "test234";// stored password for that user
        } else if (username.equals("sunil")) { // company director without any username policy
            storedPassword = "sunil";// stored password for that user
        } else {
            storedPassword = "";
        }
        return storedPassword;
    }
}
